package com.bridgeintelligent.tag.customer.ocdsm;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author：liwei
 * @Date：2023/11/17 14:00
 * @Desc：
 */

public class GlobalIncr {
    private static AtomicInteger counter = new AtomicInteger(0);
    private static ScheduledExecutorService scheduler;

    public GlobalIncr() {
        // 创建一个定时器，每天的0点执行任务
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(() -> {
            // 每天的0点将计数器重置为0
            counter.set(0);
            System.out.println("Shared cache reset to 0");
        }, getDelay(0), getDelay(24), TimeUnit.HOURS);
    }

    public static int getAndIncrement() {
        int currentValue = counter.get();
        counter.incrementAndGet();
        return currentValue;
    }

    private static long getDelay(int hours) {
        return (long) hours * 60 * 60 * 1000; // 将小时转换为毫秒延迟
    }
}

